// DatastoreItem.cpp : Implementation of CDatastoreItem

#include "stdafx.h"
#include "DatastoreItem.h"
#include "Fields.h"

// CDatastoreItem

STDMETHODIMP CDatastoreItem::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IDatastoreItem
	};

	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CDatastoreItem::Edit(void)
{
	//m_Revision = 
	return S_OK;
}

STDMETHODIMP CDatastoreItem::Save(void)
{
	// TODO: Add your implementation code here

	

	return S_OK;
}

STDMETHODIMP CDatastoreItem::get_Fields(IFields** pVal)
{
	CComObject<CFields> * pFields = GetComObject<CFields>();
	
	// TODO: Add your implementation code here

	return S_OK;
}

STDMETHODIMP CDatastoreItem::get_Type(TsDatastoreItemTypeEnum* pVal)
{
	*pVal = m_Type;
	return S_OK;
}

STDMETHODIMP CDatastoreItem::get_Revision(LONG* pVal)
{
	*pVal = m_Revision;
	return S_OK;
}


STDMETHODIMP CDatastoreItem::get_Id(INT* pVal)
{
	*pVal = m_Id;
	return S_OK;
}

HRESULT CDatastoreItem::Init(CDatastore * pDs, TsDatastoreItemTypeEnum type, INT id)
{
	HRESULT hr;
	m_pDs = pDs;
	m_Id = id;
	m_Type = type;

	ADODB::_RecordsetPtr spRS;
	hr = m_pDs->m_pDBHelper->GetDataItem(m_Type, m_Id, spRS);
	if (FAILED(hr))
	{
		BSTR err = GetLastErrorString();
		Error(err);
		return hr;
	}

	//CComObject<CFields> * pFields;// = GetComObject<CFields>();
	IFields * pFields;
	get_Fields(&pFields);
	LONG count;
	pFields->get_Count(&count);
	for (LONG i=0; i<count; i++)
	{
		IField * pField;
		pFields->get_Item(_variant_t(i), &pField);
		_bstr_t fieldName;
		pField->get_Name(fieldName.GetAddress());
		spRS->Fields->Item[fieldName]->Value;

//		CComObject<CField> * pField = GetComObject<CField>();
//		pField->
//		pField->Release();
	}
	pFields->Release();
	
	return S_OK;
}
